package com.youyi.yyfilepickerlibrary.core.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lkx.liandianqinew.R;
import com.youyi.yyfilepickerlibrary.core.SelectOptions;
import com.youyi.yyfilepickerlibrary.core.adapter.BreadAdapter;
import com.youyi.yyfilepickerlibrary.core.adapter.FileListAdapter;
import com.youyi.yyfilepickerlibrary.core.adapter.SelectSdcardAdapter;
import com.youyi.yyfilepickerlibrary.core.model.BreadModel;
import com.youyi.yyfilepickerlibrary.core.model.EssFile;
import com.youyi.yyfilepickerlibrary.core.model.EssFileCountCallBack;
import com.youyi.yyfilepickerlibrary.core.model.EssFileListCallBack;
import com.youyi.yyfilepickerlibrary.core.task.EssFileCountTask;
import com.youyi.yyfilepickerlibrary.core.task.EssFileListTask;
import com.youyi.yyfilepickerlibrary.core.util.Const;
import com.youyi.yyfilepickerlibrary.core.util.FileUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SelectFileByBrowserActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener, View.OnClickListener, EssFileListCallBack, EssFileCountCallBack, FileListAdapter.onLoadFileCountListener {
    private EssFileCountTask essFileCountTask;
    private EssFileListTask essFileListTask;
    private FileListAdapter mAdapter;
    private BreadAdapter mBreadAdapter;
    private RecyclerView mBreadRecyclerView;
    private TextView mBtFolder;
    private boolean mCanPreview = true;
    private MenuItem mCountMenuItem;
    private String mCurFolder = (Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator);
    private boolean mHasChangeSdCard = false;
    private ImageView mImbSelectSdCard;
    private RecyclerView mRecyclerView;
    private List<String> mSdCardList;
    private PopupWindow mSelectSdCardWindow;
    private int mSelectSortTypeIndex = 0;
    private ArrayList<EssFile> mSelectedFileList = new ArrayList<>();
    private Toolbar mToolBar;

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.SupportActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_select_file);
        List<String> allSdPaths = FileUtils.getAllSdPaths(this);
        this.mSdCardList = allSdPaths;
        if (!allSdPaths.isEmpty()) {
            this.mCurFolder = this.mSdCardList.get(0) + File.separator;
        }
        initUi();
        initData();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.browse_menu, menu);
        MenuItem findItem = menu.findItem(R.id.browser_select_count);
        this.mCountMenuItem = findItem;
        findItem.setTitle(String.format(getString(R.string.selected_file_count), String.valueOf(this.mSelectedFileList.size()), String.valueOf(SelectOptions.getInstance().maxCount)));
        if (SelectOptions.getInstance().isFolder) {
            this.mCountMenuItem.setVisible(false);
        } else {
            this.mCountMenuItem.setVisible(true);
        }
        return true;
    }

    private void initUi() {
        this.mBtFolder = (TextView) findViewById(R.id.id_chose_folder);
        if (SelectOptions.getInstance().isFolder) {
            this.mBtFolder.setVisibility(View.VISIBLE);
            TextView textView = this.mBtFolder;
            textView.setText("选择此文件夹：" + new File(this.mCurFolder).getName());
        } else {
            this.mBtFolder.setVisibility(View.GONE);
        }
        this.mBtFolder.setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByBrowserActivity.AnonymousClass1 */

            public void onClick(View view) {
                Log.d("SelectFileByBrowserActi", "mCurFolder:" + SelectFileByBrowserActivity.this.mCurFolder);
                Intent intent = new Intent();
                intent.putExtra(Const.FOLDER_PATH, SelectFileByBrowserActivity.this.mCurFolder);
                SelectFileByBrowserActivity.this.setResult(-1, intent);
                SelectFileByBrowserActivity.this.finish();
            }
        });
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        this.mToolBar = toolbar;
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("文件选择");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        this.mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByBrowserActivity.AnonymousClass2 */

            public void onClick(View view) {
                SelectFileByBrowserActivity.this.onBackPressed();
            }
        });
        this.mRecyclerView = (RecyclerView) findViewById(R.id.rcv_file_list);
        this.mBreadRecyclerView = (RecyclerView) findViewById(R.id.breadcrumbs_view);
        ImageView imageView = (ImageView) findViewById(R.id.imb_select_sdcard);
        this.mImbSelectSdCard = imageView;
        imageView.setOnClickListener(this);
        if (!this.mSdCardList.isEmpty() && this.mSdCardList.size() > 1) {
            this.mImbSelectSdCard.setVisibility(View.VISIBLE);
        }
        this.mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        FileListAdapter fileListAdapter = new FileListAdapter(new ArrayList());
        this.mAdapter = fileListAdapter;
        fileListAdapter.setLoadFileCountListener(this);
        this.mRecyclerView.setAdapter(this.mAdapter);
        this.mAdapter.bindToRecyclerView(this.mRecyclerView);
        this.mAdapter.setOnItemClickListener(this);
        this.mBreadRecyclerView.setLayoutManager(new LinearLayoutManager(this, 0, false));
        BreadAdapter breadAdapter = new BreadAdapter(new ArrayList());
        this.mBreadAdapter = breadAdapter;
        this.mBreadRecyclerView.setAdapter(breadAdapter);
        this.mBreadAdapter.bindToRecyclerView(this.mBreadRecyclerView);
        this.mBreadAdapter.setOnItemChildClickListener(this);
    }

    private void initData() {
        executeListTask(this.mSelectedFileList, this.mCurFolder, SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void executeListTask(List<EssFile> list, String str, String[] strArr, int i) {
        EssFileListTask essFileListTask2 = new EssFileListTask(list, str, strArr, i, this);
        this.essFileListTask = essFileListTask2;
        essFileListTask2.execute(new Void[0]);
    }

    @Override // com.youyi.yyfilepickerlibrary.core.model.EssFileListCallBack
    public void onFindFileList(String str, List<EssFile> list) {
        if (list.isEmpty()) {
            this.mAdapter.setEmptyView(R.layout.empty_file_list);
        }
        this.mCurFolder = str;
        TextView textView = this.mBtFolder;
        textView.setText("选择此文件夹：" + new File(this.mCurFolder).getName());
        this.mAdapter.setNewData(list);
        List<BreadModel> breadModeListFromPath = FileUtils.getBreadModeListFromPath(this.mSdCardList, this.mCurFolder);
        if (this.mHasChangeSdCard) {
            this.mBreadAdapter.setNewData(breadModeListFromPath);
            this.mHasChangeSdCard = false;
        } else if (breadModeListFromPath.size() > this.mBreadAdapter.getData().size()) {
            this.mBreadAdapter.addData((Collection) BreadModel.getNewBreadModel(this.mBreadAdapter.getData(), breadModeListFromPath));
        } else {
            int removedBreadModel = BreadModel.getRemovedBreadModel(this.mBreadAdapter.getData(), breadModeListFromPath);
            if (removedBreadModel > 0) {
                BreadAdapter breadAdapter = this.mBreadAdapter;
                breadAdapter.setNewData(breadAdapter.getData().subList(0, removedBreadModel));
            }
        }
        this.mBreadRecyclerView.smoothScrollToPosition(this.mBreadAdapter.getItemCount() - 1);
        this.mRecyclerView.scrollToPosition(0);
        this.mRecyclerView.scrollBy(0, ((BreadModel) this.mBreadAdapter.getData().get(this.mBreadAdapter.getData().size() - 1)).getPrePosition());
    }

    private void showPopupWindow() {
        PopupWindow popupWindow = this.mSelectSdCardWindow;
        if (popupWindow != null) {
            popupWindow.showAsDropDown(this.mImbSelectSdCard);
            return;
        }
        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_select_sdcard, (ViewGroup) null);
        PopupWindow popupWindow2 = new PopupWindow(inflate, -1, -2);
        this.mSelectSdCardWindow = popupWindow2;
        popupWindow2.setFocusable(true);
        this.mSelectSdCardWindow.setOutsideTouchable(true);
        RecyclerView recyclerView = (RecyclerView) inflate.findViewById(R.id.rcv_pop_select_sdcard);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final SelectSdcardAdapter selectSdcardAdapter = new SelectSdcardAdapter(FileUtils.getAllSdCardList(this.mSdCardList));
        recyclerView.setAdapter(selectSdcardAdapter);
        selectSdcardAdapter.bindToRecyclerView(recyclerView);
        selectSdcardAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByBrowserActivity.AnonymousClass3 */

            @Override // com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SelectFileByBrowserActivity.this.mSelectSdCardWindow.dismiss();
                SelectFileByBrowserActivity.this.mHasChangeSdCard = true;
                SelectFileByBrowserActivity selectFileByBrowserActivity = SelectFileByBrowserActivity.this;
                selectFileByBrowserActivity.executeListTask(selectFileByBrowserActivity.mSelectedFileList, FileUtils.getChangeSdCard((String) selectSdcardAdapter.getData().get(i), SelectFileByBrowserActivity.this.mSdCardList), SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
            }
        });
        this.mSelectSdCardWindow.showAsDropDown(this.mImbSelectSdCard);
    }

    public void backgroundAlpha(float f) {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = f;
        getWindow().setAttributes(attributes);
    }

    @Override // com.youyi.yyfilepickerlibrary.core.model.EssFileCountCallBack
    public void onFindChildFileAndFolderCount(int i, String str, String str2) {
        ((EssFile) this.mAdapter.getData().get(i)).setChildCounts(str, str2);
        this.mAdapter.notifyItemChanged(i, "childCountChanges");
    }

    @Override // com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        if (baseQuickAdapter.equals(this.mAdapter)) {
            EssFile essFile = (EssFile) this.mAdapter.getData().get(i);
            if (essFile.isDirectory()) {
                ((BreadModel) this.mBreadAdapter.getData().get(this.mBreadAdapter.getData().size() - 1)).setPrePosition(this.mRecyclerView.computeVerticalScrollOffset());
                ArrayList<EssFile> arrayList = this.mSelectedFileList;
                executeListTask(arrayList, this.mCurFolder + essFile.getName() + File.separator, SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
            } else if (SelectOptions.getInstance().isSingle) {
                this.mSelectedFileList.add(essFile);
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, this.mSelectedFileList);
                setResult(-1, intent);
                super.onBackPressed();
            } else {
                if (((EssFile) this.mAdapter.getData().get(i)).isChecked()) {
                    int findFileIndex = findFileIndex(essFile);
                    if (findFileIndex != -1) {
                        this.mSelectedFileList.remove(findFileIndex);
                    }
                } else if (this.mSelectedFileList.size() >= SelectOptions.getInstance().maxCount) {
                    RecyclerView recyclerView = this.mRecyclerView;
                    Snackbar.make(recyclerView, "您最多只能选择" + SelectOptions.getInstance().maxCount + "个。", -1).show();
                    return;
                } else {
                    this.mSelectedFileList.add(essFile);
                }
                ((EssFile) this.mAdapter.getData().get(i)).setChecked(!((EssFile) this.mAdapter.getData().get(i)).isChecked());
                this.mAdapter.notifyItemChanged(i, "");
                this.mCountMenuItem.setTitle(String.format(getString(R.string.selected_file_count), String.valueOf(this.mSelectedFileList.size()), String.valueOf(SelectOptions.getInstance().maxCount)));
            }
        }
    }

    private int findFileIndex(EssFile essFile) {
        for (int i = 0; i < this.mSelectedFileList.size(); i++) {
            if (this.mSelectedFileList.get(i).getAbsolutePath().equals(essFile.getAbsolutePath())) {
                return i;
            }
        }
        return -1;
    }

    @Override // android.support.v4.app.FragmentActivity
    public void onBackPressed() {
        if (!FileUtils.canBackParent(this.mCurFolder, this.mSdCardList)) {
            super.onBackPressed();
            return;
        }
        ArrayList<EssFile> arrayList = this.mSelectedFileList;
        executeListTask(arrayList, new File(this.mCurFolder).getParentFile().getAbsolutePath() + File.separator, SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
    }

    @Override // com.chad.library.adapter.base.BaseQuickAdapter.OnItemChildClickListener
    public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        if (baseQuickAdapter.equals(this.mBreadAdapter) && view.getId() == R.id.btn_bread) {
            String breadModelListByPosition = FileUtils.getBreadModelListByPosition(this.mSdCardList, this.mBreadAdapter.getData(), i);
            if (!this.mCurFolder.equals(breadModelListByPosition)) {
                executeListTask(this.mSelectedFileList, breadModelListByPosition, SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
            }
        }
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onDestroy() {
        super.onDestroy();
        EssFileListTask essFileListTask2 = this.essFileListTask;
        if (essFileListTask2 != null) {
            essFileListTask2.cancel(true);
        }
        EssFileCountTask essFileCountTask2 = this.essFileCountTask;
        if (essFileCountTask2 != null) {
            essFileCountTask2.cancel(true);
        }
    }

    public void onClick(View view) {
        if (view.getId() == R.id.imb_select_sdcard) {
            showPopupWindow();
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int itemId = menuItem.getItemId();
        if (itemId == R.id.browser_select_count) {
            if (this.mSelectedFileList.isEmpty()) {
                return true;
            }
            Intent intent = new Intent();
            intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, this.mSelectedFileList);
            setResult(-1, intent);
            super.onBackPressed();
        } else if (itemId == R.id.browser_sort) {
            new AlertDialog.Builder(this).setSingleChoiceItems(R.array.sort_list, 0, new DialogInterface.OnClickListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByBrowserActivity.AnonymousClass6 */

                public void onClick(DialogInterface dialogInterface, int i) {
                    SelectFileByBrowserActivity.this.mSelectSortTypeIndex = i;
                }
            }).setNegativeButton("降序", new DialogInterface.OnClickListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByBrowserActivity.AnonymousClass5 */

                public void onClick(DialogInterface dialogInterface, int i) {
                    int i2 = SelectFileByBrowserActivity.this.mSelectSortTypeIndex;
                    if (i2 == 0) {
                        SelectOptions.getInstance().setSortType(1);
                    } else if (i2 == 1) {
                        SelectOptions.getInstance().setSortType(2);
                    } else if (i2 == 2) {
                        SelectOptions.getInstance().setSortType(5);
                    } else if (i2 == 3) {
                        SelectOptions.getInstance().setSortType(7);
                    }
                    ((BreadModel) SelectFileByBrowserActivity.this.mBreadAdapter.getData().get(SelectFileByBrowserActivity.this.mBreadAdapter.getData().size() - 1)).setPrePosition(0);
                    SelectFileByBrowserActivity selectFileByBrowserActivity = SelectFileByBrowserActivity.this;
                    selectFileByBrowserActivity.executeListTask(selectFileByBrowserActivity.mSelectedFileList, SelectFileByBrowserActivity.this.mCurFolder, SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
                }
            }).setPositiveButton("升序", new DialogInterface.OnClickListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectFileByBrowserActivity.AnonymousClass4 */

                public void onClick(DialogInterface dialogInterface, int i) {
                    int i2 = SelectFileByBrowserActivity.this.mSelectSortTypeIndex;
                    if (i2 == 0) {
                        SelectOptions.getInstance().setSortType(0);
                    } else if (i2 == 1) {
                        SelectOptions.getInstance().setSortType(3);
                    } else if (i2 == 2) {
                        SelectOptions.getInstance().setSortType(4);
                    } else if (i2 == 3) {
                        SelectOptions.getInstance().setSortType(6);
                    }
                    ((BreadModel) SelectFileByBrowserActivity.this.mBreadAdapter.getData().get(SelectFileByBrowserActivity.this.mBreadAdapter.getData().size() - 1)).setPrePosition(0);
                    SelectFileByBrowserActivity selectFileByBrowserActivity = SelectFileByBrowserActivity.this;
                    selectFileByBrowserActivity.executeListTask(selectFileByBrowserActivity.mSelectedFileList, SelectFileByBrowserActivity.this.mCurFolder, SelectOptions.getInstance().getFileTypes(), SelectOptions.getInstance().getSortType());
                }
            }).setTitle("请选择").show();
        }
        return true;
    }

    @Override // com.youyi.yyfilepickerlibrary.core.adapter.FileListAdapter.onLoadFileCountListener
    public void onLoadFileCount(int i) {
        EssFileCountTask essFileCountTask2 = new EssFileCountTask(i, ((EssFile) this.mAdapter.getData().get(i)).getAbsolutePath(), SelectOptions.getInstance().getFileTypes(), this);
        this.essFileCountTask = essFileCountTask2;
        essFileCountTask2.execute(new Void[0]);
    }
}
